<%@page import="marioarena.DbConnection"%>
<%@ page import="com.mongodb.*" language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<link rel="stylesheet" type="text/css" href="style/layout.css" />
	<link rel="stylesheet" type="text/css" href="style/serverlist.css" />
	<link rel="stylesheet" type="text/css" href="style/serverdetails.css" />
	<link rel="stylesheet" type="text/css" href="style/login.css" />
	<script type="text/javascript" src="libs/jquery.js"></script>  
	<script language="JavaScript" src="applet/launch.js"></script>
	<script language="JavaScript" src="javascript/serverselection.js"></script>
	<script language="JavaScript" src="javascript/login-dropdown.js"></script>
	
	
	<title>MarioArena - BattleLag</title>
</head>
<body>
<APPLET name="gameLaunchApplet" CODE="GameLauncher" ARCHIVE="applet/gamelaunchapplet.jar" WIDTH=0 HEIGHT=0></APPLET>

<div id="container">
	<div id="login_bounds">
	<div id="login_frame">
				<%
					String username = (String) session.getAttribute("username");
					boolean loggedIn = false;
					
					if (username != null){
						if (!username.equals("")){ // Check if logged in
							loggedIn = true;
						}
					}
					
					// NOT logged in; show a login form
					if(!loggedIn){
						out.print("<a href='#' id='loginswitch' onclick='slide()'>login</a>");
						out.print(
								"<div id='login'><form action='/MarioArena_Website/login' method='post'>" +
									"<label class='login_label' for='username'>Username </label><input type='text' class='login_field' name='username'/>" +
									"<label class='login_label' for='password'>Password </label><input type='password' class='login_field' name='password'/>" +
									"<input type='submit' class='register_submit' value=' Sign in '/>" + 
											"<a href='/MarioArena_Website/register'>register</a></div>" +
								"</form>");
					}

					// Logged in; show the username and score
					else{
						//Show user id in hidden input so it can be retrieved by javascript
						String id = (String) session.getAttribute("id");
						DbConnection dbConnection = new DbConnection();
						//dbConnection.updateScore(id, 5); //<------- test for adding score
						int score = dbConnection.getScore(id);
						String scoreString = Integer.toString(score);
						if(score == -1)
							scoreString = "not found";
					
						out.print("<div id='loggedin'>");
						out.print("<input type='hidden' id='username' value='" + username + "' name='username' />");
						out.print("<input type='hidden' id='webid' value='" + id + "' name='webid' />");
						out.print("<p id='loggedin_username'>Logged in as: <b>" + username + "</b><br/>score: <b>" + scoreString 
								+ "</b><br/><a href='/MarioArena_Website/logout'>Sign out</a></p>");
						out.print("</div>");
					}
				%>
			</div>
	</div>
	<div id="bounds">
		
		<div id="banner">
			
		</div>
		<div id="content">
			<div id="top-content">
				<div id="navigation"></div>
				<div id="servers-banner"></div>
			</div>
			
			<div id="inner-content">
				<div id="filter-top-line"></div>
				<div id="filters">
					<div id="filter-banner"></div>
					<div id="filter-selection"></div>
					<div id="filter-apply"></div>
				</div>
				
				<div id="mid-margin">
				</div>
			</div>
			
			<div id="bottom-content">
				<div id="servers">
					<div id="server-list">
						<div id="server-header">
							<div class="label-name"><span>Server</span></div>
							<div class="label-ip"><span>IP-address</span></div>
							<div class="label-port"><span>Port</span></div>
							<div class="label-players"><span>Players</span></div>
							<div class="label-state"><span>Status</span></div>
						</div>
						<div id="server-items">
							<!-- FOR PHP/MYSQL VERSION [phpopen] php include('scripts/viewservers.php') [phpclose]-->
							<% 
							// Retrieve the server list from the servlet
							String data=(String)request.getAttribute("serverlisthtml");
							out.println(data);
							%>
						</div>
					</div>

					<div id="server-details">
						<div id="server-name"><br/></div>
						<div id="server-button"><a onclick="onClickJoinServer()"></a></div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<%  
	// Show javascript popup if a login attempt has failed
	String loginAttempt = request.getParameter("login");
	if (loginAttempt != null && loginAttempt.equals("failed"))
	{
		out.print("<script type='text/javascript'>alert(\"Login failed.\")</script>");
	} 
%>
</body>
</html>